package com.rain.shoppingscienceproj.ui.activity.order;

import android.os.Bundle;
import android.widget.EditText;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.http.BaseSubscriber;
import com.rain.shoppingscienceproj.http.ExceptionHandle;
import com.rain.shoppingscienceproj.http.RetrofitClient;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.base.GlobalData;
import com.rain.shoppingscienceproj.ui.utile.TitleUtils;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.LoadingDialog;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;

public class DeliveredActivity extends BaseActivity {

    String orderId = "";

    @BindView(R.id.et_kuai_di)
    EditText et_kuai_di;
    @BindView(R.id.et_kuai_no)
    EditText et_kuai_no;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delivered);
        userInfo = GlobalData.getUserInfo();
        AppManager.getAppManager().addActivity(this);
        loadingDialog = new LoadingDialog(this, "正在加载...");
        ButterKnife.bind(this);
        TitleUtils.TitleRightText(this, "订单发货", "发货");
        orderId = getIntent().getStringExtra("type");
    }

    @OnClick(R.id.rl_right)
    void save() {
        String kuaidi = et_kuai_di.getText().toString().trim();
        if (kuaidi == null || kuaidi.equals("")) {
            ToastUtils.showShortToast("请输入快递单位");
        }
        String kuaidi_no = et_kuai_no.getText().toString().trim();
        if (kuaidi_no == null || kuaidi_no.equals("")) {
            ToastUtils.showShortToast("请输入运单号");
        }
        loadingDialog.show();
        Map<String, String> map = new HashMap<>();
        map.put("userId", userInfo.getUserId());
        map.put("token", userInfo.getToken());
//        map.put("shopId", userInfo.getShopId());
        map.put("orderId", orderId);
        map.put("kdname", kuaidi);
        map.put("kdno", kuaidi_no);
        map.put("status", "3");
        RetrofitClient.getInstance(this)
                .createBaseApi()
                .sureOrder(map, new BaseSubscriber<ResponseBody>(this) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        ToastUtils.showShortToast("网络错误");
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        super.onNext(responseBody);
                        loadingDialog.dismiss();
                        try {
                            String request = responseBody.string();
                            JSONObject json = new JSONObject(request);
                            int status = json.getInt("code");
                            if (status == 200) {
                                finish();
                            } else {
                                ToastUtils.showShortToast(json.getString("msg"));
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
